/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.event.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.course.service.CourseService;
import com.fanrenqiji.modules.business.course.vo.CourseVO;
import com.fanrenqiji.modules.business.semester.service.SemesterService;
import com.fanrenqiji.modules.business.semester.vo.SemesterVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.event.vo.EventVO;
import com.fanrenqiji.modules.business.event.service.EventService;

@Controller
@RequestMapping(value = "${adminPath}/business/event")
public class EventController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(EventController.class);
    @Autowired
    private EventService eventService;
    @Autowired
    private SemesterService semesterService;

    @ModelAttribute("event")
    public EventVO getEvent(@RequestParam(required = false) String id) {
        if (StringUtils.isNotEmpty(id)) {
            return eventService.get(id);
        } else {
            return new EventVO();
        }
    }

    /**
     * 列表
     *
     * @return
     */
    @RequiresPermissions("business:event:view")
    @RequestMapping(value = {"list", ""})
    public String list(EventVO event, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
        Page<EventVO> page = eventService.findEventByPage(new Page<EventVO>(request, response),
                event);
        model.addAttribute("page", page);
        model.addAttribute("event", event);
        return "modules/business/event/eventList";
    }

    /**
     * 列表
     *
     * @return
     */
    @RequiresPermissions("business:event:begin")
    @RequestMapping(value = {"list2"})
    public String list2(EventVO event, HttpServletRequest request,
                        HttpServletResponse response, Model model,String semesterId) {
        if (StringUtils.isNotBlank(semesterId)){
            SemesterVO semesterVO = semesterService.get(semesterId);
            model.addAttribute("semesterId", semesterId);
            model.addAttribute("semesterName", semesterVO.getNumber());
            event.setFormal(semesterVO.getFormal());
        }

        Page<EventVO> page = eventService.findEventByPage(new Page<EventVO>(request, response), event);
        model.addAttribute("page", page);
        model.addAttribute("event", event);

        return "modules/business/event/eventList2";
    }

    /**
     * 选择列表
     *
     * @return
     */
    @RequestMapping(value = "select")

    public String select(String courseid, EventVO event, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<EventVO> page = eventService.findEventByPage(new Page<EventVO>(request, response, 10), event);
        model.addAttribute("page", page);
        model.addAttribute("event", event);
        return "modules/business/event/eventSelect";
    }

    /**
     * 用户添加更新表
     *
     * @param model
     * @return
     */
    @RequiresPermissions("business:event:view")
    @RequestMapping(value = "form")
    public String form(Model model, EventVO event) {
        if (StringUtils.isNotEmpty(event.getId())) {
            model.addAttribute("event", eventService.get(event.getId()));
        } else {
            model.addAttribute("event", event);
        }
        return "modules/business/event/eventForm";
    }

    /**
     * 添加或修改提交
     *
     * @param event
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("business:event:edit")
    @RequestMapping(value = "save")
    public String save(EventVO event, Model model,
                       RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
        if (!beanValidator(model, event)) {
            return form(model, event);
        }
        eventService.update(event);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + GlobalSetting.getAdminPath()
                + "/business/event/?repage";
    }

    /**
     * 删除操作
     *
     * @return
     */
    @RequestMapping("delete")
    @RequiresPermissions("business:event:edit")
    public String delete(String id, RedirectAttributes redirectAttributes) {
        eventService.delete(id);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/event/";
    }


    /**
     * 添加问题的请求的页面
     *
     * @param eventVO
     * @param model
     * @return
     */
    @RequestMapping(value = "addquestion")
    @RequiresPermissions("business:event:edit")
    // 事件里面添加问题的方法 有两个类型 一个是问题 一个是测试 需要在业务层返回的时候进行判定
    public String addquestion(EventVO eventVO, Model model) {
        // 先进行非空判定
        if (StringUtils.isNotBlank(eventVO.getId())) {
            eventVO = eventService.get(eventVO.getId());
            model.addAttribute("event", eventVO);
        } else {
            model.addAttribute("event", null);
        }
        return "modules/business/event/addquestion";
    }


    /**
     * 事件开始的方法
     */
    @RequestMapping("begin")
    public String begin(EventVO eventVO,String semesterId) {

        eventService.begin(eventVO, semesterId);
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/event/list2?semesterId="+semesterId;
    }

    /**
     * 事件结束的方法
     *
     * @param eventVO
     */
    @RequestMapping("end")
    public String end(EventVO eventVO,String semesterId) {
        eventService.end(eventVO,semesterId);
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/event/list2?semesterId="+semesterId;
    }

    @RequestMapping("kaishishangke")
    @ResponseBody
    public String kaishishangke(String content, String time,String semesterId) {
        eventService.kaishishangke(content, time,semesterId);
        return "1";
    }

    @RequestMapping("kechengyugao")
    @ResponseBody
    public String kechengyugao(String semesterId,String time){
        eventService.kechengyugao(semesterId,time);
        return "1";
    }
}